package util.android.cuestionario.fragment;

import com.androidutil.R;

import util.android.window.OnAceptarCancelarListener;
import util.form.CuestionarioContext;
import util.form.respuesta.RegistroImpl;
import util.form.respuesta.listener.OnGuardarRegistroListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;

//FRAGMENT: Minimo API 11
//http://developer.android.com/training/animation/screen-slide.html
public abstract class CuestionarioFragment 
extends Fragment
implements PageViewListener
{

	private int position;
	//private final int layoutId;
	
    public CuestionarioFragment() {    	
    	
    	//this.layoutId = getLayoutId();
    	
    	//se retiene instancia ante un cambio de orientacion del dispositvo
		setRetainInstance(true);
	}
    
    public void setPosition(int position) {
    	this.position = position;
    }
    
    public int getLayoutId() {
    	return getLayoutIdDefault();
    }
    
    protected final int getLayoutIdDefault() {
    	return R.layout.fin_seccion;
    }
    
	@Override
    public final View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {		
		int layoutId = getLayoutId();
		
		ScrollView sv = (ScrollView) inflater.inflate(R.layout.scroll_view, container, false);	
		LinearLayout ly = (LinearLayout) sv.findViewById(R.id.layoutScroll);
		
        //ViewGroup rootView = (ViewGroup) inflater.inflate(layoutId, container, false);
		ViewGroup rootView = (ViewGroup) View.inflate(ly.getContext(), layoutId, null);
        ly.addView(rootView);
		//ViewUtil.addViewMatch_Wrap(ly,rootView);
        
        try {
        	if(layoutId == getLayoutIdDefault()) {
        		//Button btn = (Button)rootView.findViewWithTag("boton");
        		//System.out.println("ID="+R.id.botonFinSeccion);
        		Button btn = (Button)rootView.findViewById(R.id.botonFinSeccion);
        		procesarOnCreateViewDefault(rootView,btn);
        	}
        	else {
		        PageView pv = new PageView(getCuestionarioContext(), position, rootView, this);
		        
		        pv.setOnGuardarListener(new OnGuardarRegistroListener() {				
		        	@Override
					public void onGuardar(RegistroImpl reg) throws Exception {
						guardar(reg);
					}
				});
		        
		        procesarOnCreateView(pv); 
		        rootView.setTag(pv);
        	}
        }
        catch(Exception e) {
        	CuestionarioFragmentActivity activity = (CuestionarioFragmentActivity) this.getActivity();
        	activity.manejarExcepcion(e);
        }
        
        return sv;
    }
	
	protected void procesarOnCreateViewDefault(ViewGroup rootView, Button btn) throws Exception {
		
	}

	protected void procesarOnCreateView(final PageView pv) throws Exception {
		
	}

	public final CuestionarioContext getCuestionarioContext() {
		return (CuestionarioContext) this.getActivity();
	}
	
	/*
	@Override
	public void mostrarMensaje(String mensaje) {
		CuestionarioFragmentActivity activity = (CuestionarioFragmentActivity) this.getActivity();
    	activity.mostrarMensaje(mensaje);
	}
	
	@Override
	public void manejarExcepcion(Exception e) {
		CuestionarioFragmentActivity activity = (CuestionarioFragmentActivity) this.getActivity();
		activity.manejarExcepcion(e);
	}
	*/

	

	@Override
	public boolean onAnteriorPagina(PageView pv) throws Exception {
		return true;
	}
	
	@Override
	public void onAntesCambioPagina(boolean avanzando, PageView pv, final CambioPagina cambioPagina) throws Exception {
		
		if(avanzando) {		
			boolean pregunta = pv.preguntarPorOmisiones(new OnAceptarCancelarListener() {				
				@Override
				public void onCerrar() throws Exception {
					
				}
				
				@Override
				public void onCancelar() throws Exception {
					cambioPagina.cancelar();
				}
				
				@Override
				public boolean onAceptar() throws Exception {
					//cambioPagina.continuar();//no funciona bien
					cambioPagina.cancelar();
					return true;
				}
			});			
			
			if(pregunta)
				cambioPagina.pausar();
		}
	}
	

	@Override
	public final boolean onSiguientePagina(final PageView pv) throws Exception {
		//AndroidUtil.mostrarToast(getActivity(), JsonUtil.toJSON(pv));		
		
		return pv.guardar();	
	}
		
	protected abstract void guardar(RegistroImpl reg) throws Exception;

	@Override
	public void onPaginaSeleccionada(boolean avanzando, PageView pv) throws Exception {
		
	}
	
	public void salir() {
		CuestionarioFragmentActivity activity = (CuestionarioFragmentActivity) this.getActivity();
		activity.salir();
	}
}